package com.wangxiaonan.berry.controller;

import com.alibaba.fastjson.JSONObject;
import com.wangxiaonan.berry.handler.ResponseWrapper;
import com.wangxiaonan.berry.service.MaxentService;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 猛犸指纹
 *
 * @Author: wangxiaonan
 * @Date: 2019/7/2
 **/
@CrossOrigin
@RestController
@RequestMapping("/maxent")
public class MaxentController {

    private static final Logger logger = LoggerFactory.getLogger(MaxentController.class);

    @Resource
    private MaxentService maxentService;

    @RequestMapping(value = "/tick", method = RequestMethod.GET)
    private ResponseWrapper query(@RequestParam String tick) {
        ResponseWrapper wrapper = new ResponseWrapper();
        String data = maxentService.query(tick);
        logger.info("猛犸返回参数：{}", data);

        JSONObject jsonObject = JSONObject.parseObject(data);
        String status = jsonObject.getString("ok");
        if ("true".equals(status)) {
            JSONObject dataObject = jsonObject.getJSONObject("data");
            wrapper.success().setData(dataObject);
        } else {
            wrapper.fail(status).setMessage("no data");
        }

        return wrapper;
    }
}
